package com.geoboard.fragments;

import com.geoboard.ActivityInterface;
import com.geoboard.FragContainerInterface.FragType;
import com.geoboard.R;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.TextView;

public class TabControlFrag extends Fragment
		implements OnTouchListener{
	
	/**** Members **************************************************/
	private boolean isTextView = false;
	
	private TextView mapTab = null;
	private TextView textTab = null;
	
	private FragType curState = null;
	
	/**** Methods **************************************************/	

	public void setState(FragType newState){		
		
		if(newState != curState && mapTab != null){
			curState = newState;
			switch(newState){
				case POST:
					mapTab.setText(R.string.post_tab_text);
					break;
			
				case BOARD:
				default:
					mapTab.setText(R.string.board_tab_text);
	
			}
		}
		
	}

	public void setIsTextView(boolean isTextView) {

		if(this.isAdded()){
			this.isTextView = isTextView;
		
			if(isTextView){
				if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
					textTab.setBackground(getResources().getDrawable(R.drawable.tab_selected));
					mapTab.setBackground(getResources().getDrawable(R.drawable.tab_unselected));	
				} else {
					textTab.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_selected));
					mapTab.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_unselected));
				}
			} else {
				if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
					mapTab.setBackground(getResources().getDrawable(R.drawable.tab_selected));
					textTab.setBackground(getResources().getDrawable(R.drawable.tab_unselected));
				} else {
					mapTab.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_selected));
					textTab.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_unselected));
				}
			}
		}
		
	}

	/**** Android Callback Methods *********************************/
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		return inflater.inflate(R.layout.frag_tab_control, container, false);			
	}
	
	@Override
	public void onAttach(Activity activity){
		super.onAttach(activity);
	}
	
	@Override
	public void onActivityCreated (Bundle savedInstanceState){
		super.onActivityCreated(savedInstanceState);		
	}
	
	@Override
	public void onViewStateRestored (Bundle savedInstanceState){
		super.onViewStateRestored(savedInstanceState);
	}
	
	@Override
	public void onStart(){
		super.onStart();	
		
		mapTab = (TextView)getActivity().findViewById(R.id.map_tab);
    	mapTab.setOnTouchListener(this);
    	
    	textTab = (TextView)getActivity().findViewById(R.id.text_tab);
    	textTab.setOnTouchListener(this);
	}
	
	@Override
	public void onResume(){
		super.onResume();		
		
	}
		
		
	
	@Override
	public void onPause(){
		super.onPause();	
		
			
	}
	
	@Override
	public void onStop(){
		super.onStop();
	}
	
	@Override
	public void onDestroyView(){
		super.onDestroyView();
		
		
	}
	
	@Override
	public void onDestroy(){
		super.onDestroy();
	}
	
	@Override 
	public void onDetach(){
		super.onDetach();
	}

	/**** OnTouchListener handlers ***********************************/
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		
		if(v.equals(mapTab)){
			((ActivityInterface)getActivity()).setIsTextView(false);			
			//setIsTextView(false);
		} else if(v.equals(textTab)){
			((ActivityInterface)getActivity()).setIsTextView(true);
			//setIsTextView(true);
		}
		
		return false;
	}
}
